﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.JointVariableLimitsEditor.CUiJVLEditorImageComboBox
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;


namespace DnProcessSimulateCommands.JointVariableLimitsEditor;

internal class CUiJVLEditorImageComboBox : ComboBox
{
  private Dictionary<object, CUiJVLEditorImageComboBox.ItemInfo> _itemInfos = new Dictionary<object, CUiJVLEditorImageComboBox.ItemInfo>();
  private int _imagePaddingLeft;
  private int _imagePaddingRight;
  private int _imagePaddingTop;
  private int _imagePaddingBottom;
  private IContainer components;

  public CUiJVLEditorImageComboBox()
  {
    this.InitializeComponent();
    this.DrawItem += new DrawItemEventHandler(this.CUiJVLEditorImageComboBox_DrawItem);
  }

  public int ImagePaddingLeft
  {
    get => this._imagePaddingLeft;
    set => this._imagePaddingLeft = value;
  }

  public int ImagePaddingRight
  {
    get => this._imagePaddingRight;
    set => this._imagePaddingRight = value;
  }

  public int ImagePaddingTop
  {
    get => this._imagePaddingTop;
    set => this._imagePaddingTop = value;
  }

  public int ImagePaddingBottom
  {
    get => this._imagePaddingBottom;
    set => this._imagePaddingBottom = value;
  }

  internal Image GetItemImage(object item)
  {
    Image itemImage = (Image) null;
    if (this._itemInfos.ContainsKey(item))
      itemImage = this._itemInfos[item].Image;
    return itemImage;
  }

  internal void SetItemImage(object item, Image image)
  {
    if (this._itemInfos.ContainsKey(item))
      this._itemInfos[item].Image = image;
    else
      this._itemInfos.Add(item, new CUiJVLEditorImageComboBox.ItemInfo()
      {
        Image = image
      });
  }

  private void CUiJVLEditorImageComboBox_DrawItem(object sender, DrawItemEventArgs e)
  {
    if (e.Index < 0 || e.Index >= this.Items.Count)
      return;
    e.DrawBackground();
    object key = this.Items[e.Index];
    if (this._itemInfos.ContainsKey(key))
    {
      Image itemImage = this.GetItemImage(key);
      if (itemImage != null)
      {
        Rectangle bounds = e.Bounds;
        int num1 = bounds.Width - this._imagePaddingLeft - this._imagePaddingRight;
        bounds = e.Bounds;
        int num2 = bounds.Height - this._imagePaddingTop - this._imagePaddingBottom;
        Rectangle rectangle = new Rectangle(0, 0, itemImage.Width <= num1 ? itemImage.Width : num1, itemImage.Height <= num2 ? itemImage.Height : num2);
        Graphics graphics = e.Graphics;
        Image image = itemImage;
        bounds = e.Bounds;
        int x = bounds.X + this._imagePaddingLeft;
        bounds = e.Bounds;
        int y = bounds.Y + this._imagePaddingTop;
        Rectangle srcRect = rectangle;
        graphics.DrawImage(image, x, y, srcRect, GraphicsUnit.Pixel);
      }
    }
    if ((e.State & DrawItemState.Focus) != DrawItemState.Focus)
      return;
    e.DrawFocusRectangle();
  }

  protected override void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    this.SuspendLayout();
    this.DrawMode = DrawMode.OwnerDrawFixed;
    this.DropDownStyle = ComboBoxStyle.DropDownList;
    this.ResumeLayout(false);
  }

  internal class ItemInfo
  {
    internal Image Image;

    internal ItemInfo() => this.Image = (Image) null;
  }
}
